package controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import model.Course;
import model.OfferedCourse;
import model.Registration;
import model.Student;
import model.Teacher;
import model.container.OfferedCourseContainer;
import model.container.RegistrationContainer;
import model.container.TeacherContainer;

public class PlanningController {
	/* Planning controller instance */
	private static PlanningController planningController = new PlanningController();
	
	/* Constructs a new planning controller */
	private PlanningController(){
	}
	
	/** Returns an instance of the planning controller */
	public static PlanningController getInstance(){
		return planningController;
	}
	
	/** Adds a course to the planning */
	public void addOfferedCourse(OfferedCourse offeredCourse) {
		// Get instance of the offered course container
		OfferedCourseContainer container = OfferedCourseContainer.getInstance();

		// Add course to planning
		container.addOfferedCourse(offeredCourse);
		
		// TODO is this the correct place to add the bi-directional reference?
		// Establish a bi-directional reference
		offeredCourse.getCourse().getOfferedcourses().add(offeredCourse);
	}

	/** Returns a string representation of offered courses for a given course */
	public String getPlannedCoursesText(Course course) {
		String result = "";
		List<OfferedCourse> plannedCourses = course.getOfferedcourses();
		for (int i = 0; i < plannedCourses.size(); i++) {
			result += plannedCourses.get(i).toString() + "\n";
		}
		return result;
	}

	/** Returns a string representation of preferred courses for a given teacher */
	public String getPreferedCoursesText(Teacher teacher) {
		String result = "";
		Collection<Course> preferedCourses = teacher.getCoursePreferences();
		Iterator<Course> preferedcourseIterator = preferedCourses.iterator();
		while (preferedcourseIterator.hasNext()) {
			result += preferedcourseIterator.next().toString() + "\n";
		}
		return result;
	}
	
	/** Returns a schedule for all students */
	public Collection<Registration> getSchedule() {
		RegistrationContainer container = RegistrationContainer.getInstance();
		Collection<Registration> registrations = container.getRegistrations();
		return registrations;
	}
	
	/** Returns a schedule for a student */
	public Collection<Registration> getSchedule(Student student) {
		RegistrationContainer container = RegistrationContainer.getInstance();
		Collection<Registration> registrations = container.getRegistrationsByStudent(student);
		return registrations;
	}
	
	/** Returns the tasks for all teachers */
	public Collection<OfferedCourse> getTasks() {
		// Create tasks
		Collection<OfferedCourse> tasks = new ArrayList<OfferedCourse>();
		
		// Get a list of registrations
		TeacherContainer teacherContainer = TeacherContainer.getInstance();
		for (Teacher teacher : teacherContainer.getTeachers()) {
			for (OfferedCourse offeredCourse : teacher.getOfferedCourses()) {
				tasks.add(offeredCourse);
			}
		}
		
		// Return tasks
		return tasks;
	}
	
	/** Returns the tasks for a teacher */
	public Collection<OfferedCourse> getTasks(Teacher teacher) {
		// Create tasks
		Collection<OfferedCourse> tasks = teacher.getOfferedCourses();
		
		// Return schedule
		return tasks;
	}
}